<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
      <!--      <el-form-item label="营地用户Id" prop="userId">
              <el-input
                v-model="queryParams.userId"
                placeholder="请输入营地用户Id"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="营地 " prop="campId">
        <el-select v-model="queryParams.campId" placeholder="请选择营地 " clearable>
          <el-option
            v-for="camp in campList"
            :key="camp.campId"
            :label="camp.campName"
            :value="camp.campId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地条件 " prop="factor">
        <el-select v-model="queryParams.factor" placeholder="营地条件 " clearable>
          <el-option
            v-for="dict in dict.type.c_camp_site_factor"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>





      <el-form-item label="营地用户 " prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择微信用户 " clearable>
          <el-option
            v-for="campUser in userList"
            :key="campUser.userId"
            :label="campUser.username"
            :value="campUser.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地用户类型 " prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择营地用户类型 " clearable>
          <el-option
            v-for="dict in dict.type.c_camp_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地类型" prop="campType">
        <el-select v-model="queryParams.campType" placeholder="请选择营地类型" clearable>
          <el-option
            v-for="dict in dict.type.c_camp_camp_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营地名称" prop="campName">
        <el-input
          v-model="queryParams.campName"
          placeholder="请输入营地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业时间类" prop="timeType">
        <el-select v-model="queryParams.timeType" placeholder="请选择营业时间类" clearable>
          <el-option
            v-for="dict in dict.type.c_camp_time_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核" prop="approve">
        <el-select v-model="queryParams.approve" placeholder="请选择审核" clearable>
          <el-option
            v-for="dict in dict.type.c_camp_approve"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['camp:camp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['camp:camp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['camp:camp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['camp:camp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 营地列表 -->
    <el-table v-loading="loading" :data="campList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="营地Id" align="center" prop="campId" />-->
      <!--      <el-table-column label="营地用户Id" align="center" prop="userId" />-->
      <el-table-column label="营地用户Id" align="center" prop="username" />
      <el-table-column label="营地用户类型 " align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="营地类型" align="center" prop="campType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_camp_type" :value="scope.row.campType"/>
        </template>
      </el-table-column>
      <el-table-column label="营地名称" align="center" prop="campName" />
      <el-table-column label="营地位置" align="center" prop="location" />
      <el-table-column label="具体地址" align="center" prop="address" />
      <el-table-column label="营业时间类" align="center" prop="timeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_time_type" :value="scope.row.timeType"/>
        </template>
      </el-table-column>
      <el-table-column label="具体时间" align="center" prop="specificTime" />
      <el-table-column label="联系人" align="center" prop="linkman" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="营地图片" align="center" prop="imageUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态 " align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="营地介绍" align="center" prop="introduce" />
      <el-table-column label="审核" align="center" prop="approve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_approve" :value="scope.row.approve"/>
        </template>
      </el-table-column>
      <el-table-column label="置顶首页" align="center" prop="show">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_camp_show" :value="scope.row.show"/>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['camp:camp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['camp:camp:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleShow(scope.row)"
            v-hasPermi="['camp:camp:show']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleApprove(scope.row)"
            v-hasPermi="['camp:camp:approve']"
            v-if="scope.row.approve!=1"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 营地审核对话框 -->
    <el-dialog title="营地审核" :visible.sync="open1" width="50%" append-to-body>
      <el-form ref="form1" :model="form1"  label-width="150px">
        <el-form-item label="营地" prop="campId">
          <el-input v-model="form1.campName"  readonly/>
        </el-form-item>
        <el-form-item label="用户" prop="userId">
          <el-input v-model="form1.username"  readonly/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form1.type"  value="审核消息" readonly/>
        </el-form-item>
        <el-form-item label="审核" prop="approve">
          <el-select v-model="form1.approve" placeholder="请选择审核">
            <el-option
              v-for="(dict,stats) in dict.type.c_camp_approve"
              :key="stats.index"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form1.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改营地管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="营地用户 " prop="userId" >
              <el-select v-model="form.userId" placeholder="请选择微信用户 " >
                <el-option
                  v-for="campUser in userList"
                  :key="campUser.userId"
                  :label="campUser.username"
                  :value="campUser.userId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="营地类型" prop="campType">
              <el-select v-model="form.campType" placeholder="请选择营地类型">
                <el-option
                  v-for="dict in dict.type.c_camp_camp_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="营地经度" prop="longitude" >
              <el-input v-model="form.longitude" placeholder="请输入营地经度" />
            </el-form-item>
            <el-form-item label="营地位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入营地位置" />
            </el-form-item>
            <el-form-item label="营业时间类" prop="timeType">
              <el-radio-group v-model="form.timeType">
                <el-radio
                  v-for="dict in dict.type.c_camp_time_type"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
              <el-input v-model="form.linkman" placeholder="请输入联系人" />
            </el-form-item>
            <el-form-item label="状态 " prop="state">
              <el-radio-group v-model="form.state">
                <el-radio
                  v-for="dict in dict.type.c_camp_state"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="审核" prop="approve">
              <el-select v-model="form.approve" placeholder="请选择审核">
                <el-option
                  v-for="dict in dict.type.c_camp_approve"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="营地用户类型 " prop="userType">
              <el-select v-model="form.userType" placeholder="请选择营地用户类型 ">
                <el-option
                  v-for="dict in dict.type.c_camp_user_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="营地名称" prop="campName">
              <el-input v-model="form.campName" placeholder="请输入营地名称" />
            </el-form-item>
            <el-form-item label="营地纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入营地纬度" />
            </el-form-item>
            <el-form-item label="具体地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入具体地址" />
            </el-form-item>
            <el-form-item label="具体时间" prop="specificTime">
              <el-input v-model="form.specificTime" placeholder="请输入具体时间 选择月份1-12" />
            </el-form-item>

            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" />
            </el-form-item>
            <el-form-item label="置顶首页" prop="show">
              <el-radio-group v-model="form.show">
                <el-radio
                  v-for="dict in dict.type.c_camp_show"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="评分" prop="score">
              <el-input v-model="form.score" placeholder="请输入评分" />
            </el-form-item>
          </el-col>

        </el-row>
        <!--        <el-form-item label="营地用户Id" prop="userId">
                  <el-input v-model="form.userId" placeholder="请输入营地用户Id" />
                </el-form-item>-->

        <el-form-item label="营地图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>

        <el-form-item label="营地介绍" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="title=='查看营地'">返 回</el-button>
        <el-button type="primary" @click="submitForm" v-if="title!='查看营地'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listCamp,approveCamp,getCamp, delCamp, addCamp, updateCamp} from "@/api/camp/camp";
  import { listCampUser } from "@/api/wechat/campUser";
  export default {
    name: "Camp",
    dicts: ['c_camp_time_type', 'c_camp_user_type', 'c_camp_show', 'c_camp_state', 'c_camp_camp_type', 'c_camp_approve'],
    data() {
      return {
        // 表单参数
        form1: {},
        //弹框
        open1:false,
        // 表单校验
        rules1:{},

        //微信用户列表
        userList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 营地管理表格数据
        campList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          userType: null,
          campType: null,
          campName: null,
          timeType: null,
          approve: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userId: [
            { required: true, message: "营地用户Id不能为空", trigger: "blur" }
          ],
          userType: [
            { required: true, message: "营地用户类型 不能为空", trigger: "change" }
          ],
          campType: [
            { required: true, message: "营地类型不能为空", trigger: "change" }
          ],
          campName: [
            { required: true, message: "营地名称不能为空", trigger: "blur" }
          ],
          longitude: [
            { required: true, message: "营地经度不能为空", trigger: "blur" }
          ],
          latitude: [
            { required: true, message: "营地纬度不能为空", trigger: "blur" }
          ],
          timeType: [
            { required: true, message: "营业时间类不能为空", trigger: "change" }
          ],
          specificTime: [
            { required: true, message: "具体时间 选择月份1-12不能为空", trigger: "blur" }
          ],
          imageUrl: [
            { required: true, message: "营地图片不能为空", trigger: "blur" }
          ],
          introduce: [
            { required: true, message: "营地介绍不能为空", trigger: "blur" }
          ],
          show: [
            { required: true, message: "置顶首页不能为空", trigger: "change" }
          ],
        }
      };
    },
    created() {
      this.getList();
      //请求后台，获取微信用户数据
      listCampUser().then(response => {
        this.userList = response.rows;
      });
    },
    methods: {
      /** 打开审核弹框*/
      handleApprove(camp){
        if (camp.approve==1){
          this.$modal.msgSuccess("该营地已经通过审核");
        }else {
          //给表单赋值
          this.form1.campName = camp.campName
          this.form1.username = camp.username
          this.form1.type = "审核消息"
          //this.form1.approve = camp.approve
          //
          this.form1.campId = camp.campId
          this.form1.userId = camp.userId
          //显示弹框
          this.open1 = true
        }
      },
      /** 关闭审核弹框*/
      cancel1(){
        //关闭弹框
        this.open1 = false
        //清空表单数据
        this.form1 = {}
      },
      /** 提交审核*/
      submitForm1() {
        this.$refs["form1"].validate(valid => {
          if (valid) {
            if (this.form1.approve>0){
              this.form1.type = "1"
              approveCamp(this.form1).then(response => {
                this.$modal.msgSuccess("审核成功");
                //关闭弹框
                this.open1 = false
                //清空表单数据
                this.form1 = {}
                //查询营地数据
                this.getList()
              });
            }else{
              //关闭弹框
              this.open1 = false
              //清空表单数据
              this.form1 = {}
            }
          }
        });
      },
      /** 查询营地管理列表 */
      getList() {
        this.loading = true;
        listCamp(this.queryParams).then(response => {
          this.campList = response.rows;
          this.total = response.total;

          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          campId: null,
          userId: null,
          userType: null,
          campType: null,
          campName: null,
          longitude: null,
          latitude: null,
          location: null,
          address: null,
          timeType: null,
          specificTime: null,
          linkman: null,
          phone: null,
          imageUrl: null,
          state: null,
          introduce: null,
          approve: null,
          show: null,
          score: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.campId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加营地管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const campId = row.campId || this.ids
        getCamp(campId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改营地管理";
        });
      },
      /** 查看按钮操作：使用新增修改弹框展示数据 */
      handleShow(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "查看营地";
        //请求后台，根据id查询
        /* const campId = row.campId || this.ids
         getCamp(campId).then(response => {
           this.form = response.data;
           this.open = true;
           this.title = "修改营地管理";
         });*/
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.campId != null) {
              updateCamp(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCamp(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const campIds = row.campId || this.ids;
        this.$modal.confirm('是否确认删除营地管理编号为"' + campIds + '"的数据项？').then(function() {
          return delCamp(campIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('camp/camp/export', {
          ...this.queryParams
        }, `camp_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

